package com.ndp.fb.walle.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ndp.fb.config.ConfigCentre;
import com.ndp.fb.constant.ConfigConst;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Jacky on 2014/11/7.
 */
@Controller
@RequestMapping("sysmonitor")
public class SysMonitorController {
    @Value("webVersion")
    private String webVersion;

    @Value("version")
    private String version;

    private final static String RANDOM_EGG="❈❤✪ி✎✍✌☏☢〠♟♞☬☫☄☊●☽☺☹√☝☞♤♡♢♧☆©§㉿♋♈♉♊♌♎♏♐♑♓♒♍☯❉❈♬❁☸✿❀ி☪♔";

    /**
     * 运维监控
     * @param callback
     * @return
     */
    @RequestMapping("getInitStat")
    public
    @ResponseBody
    String getInitStat(String callback) {
        //callback({"Redis":0,"InitStatus":0,"CodeVersion":"1.2","Http":0,"Mysql":0})
        Map<String, String> result = new HashMap();
        version = ConfigCentre.getString("version");
        if (!StringUtils.hasText(version)) version = "";
        result.put("Redis", "0");
        result.put("InitStatus", "0");
        result.put("CodeVersion", version);
        result.put("Http", "0");
        result.put("Mysql", "0");
        if (!StringUtils.hasText(callback)) {
            return JSONObject.toJSONString(result);
        } else {
            return callback + "(" + JSONObject.toJSONString(result) + ")";
        }
    }

    /**
     * 获取版本信息
     * @param callback
     * @return
     */
    @RequestMapping("webVersion")
    public
    @ResponseBody
    String webVersion(String callback) {
        //{"version" : "v1.0", "js" : "app/libs/require.js", "main" : "app/js/main.js"}
        Map<String, Object> result = new HashMap();
        webVersion = ConfigCentre.getString("webVersion");
        if (!StringUtils.hasText(webVersion)) webVersion = "";
        result.put("version", webVersion);
        result.put("service",ConfigCentre.getString(ConfigConst.WEB_SERVICE).split(","));
        if (!StringUtils.hasText(callback)) {
            return JSONObject.toJSONString(result);
        } else {
            return callback + "(" + JSONObject.toJSONString(result) + ")";
        }
    }
    //CDN 加速使用
    @RequestMapping("version")
    @ResponseBody
    public String version() {
        Map<String, Object> result = new HashMap();
        try {
            webVersion = ConfigCentre.getString("webVersion");
            if (!StringUtils.hasText(webVersion)) webVersion = "";
            result.put("version", webVersion);
            result.put("service",ConfigCentre.getString(ConfigConst.WEB_SERVICE).split(","));
        }catch (Exception e){
            result.put("version", "");
            result.put("service","");
        }
        return JSON.toJSONString(result);
    }
    @RequestMapping("speedtest")
    @ResponseBody
    public String speedTest() {
        return  org.apache.commons.lang3.StringUtils.rightPad("✈",1024,RANDOM_EGG.charAt(RandomUtils.nextInt(0,RANDOM_EGG.length())));
    }
}
